package resposta;

import java.io.FileReader;
import java.util.Properties;

import requisicao.Requisicao;

public class Mime {
	private String arquivo = "mimeTypes.properties";
	private String extensao = "", mimeType="noMime";
	private Requisicao requisicao;
	
	public Mime(Requisicao requisicao){		
		this.requisicao = requisicao;
		executa();
	}
	
	private void executa() {
		processarExtensao();
		processarMimeType();
	}

	private void processarExtensao() {
		String parts[] = requisicao.getCabecalho().getRecurso().split("\\.");
		String part[] = parts[parts.length-1].split("\\?");
		extensao = part[0].trim();
		System.out.println("extensao: "+extensao);
	}

	private void processarMimeType(){	
		if(extensao.equalsIgnoreCase("xpto")){
			mimeType = "text/html";
		}else{
			try(FileReader reader = new FileReader(arquivo)) {
				Properties properties = new Properties();
				properties.load(reader);
				mimeType =  properties.getProperty("."+extensao);
			} catch (Exception e) {
			}
		}
	}
	public String getMimeType(){
		return mimeType;
	}
	public String getExtensao(){
		return extensao;
	}
}
